package cn.testfan.apitest.http;

import java.io.IOException;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;

public class HttpUtil {
	public static CloseableHttpResponse get(CloseableHttpClient httpclient, String url, Header... headers)
			throws ClientProtocolException, IOException {
		HttpGet httpget = new HttpGet(url);
		System.out.println(httpget.getURI());
		for (Header header : headers) {
			httpget.addHeader(header);
		}
		return httpclient.execute(httpget);
	}

	public static void printCookie(BasicCookieStore cookieStore) {
		List<Cookie> cookies = cookieStore.getCookies();
		if (cookies.isEmpty()) {
			System.out.println("None");
		} else {
			for (int i = 0; i < cookies.size(); i++) {
				System.out.println("- " + cookies.get(i).toString());
			}
		}
	}

	public static void printHeaders(CloseableHttpResponse reponse) {
		Header[] headers = reponse.getAllHeaders();
		System.out.println("status Line:\t" + reponse.getStatusLine());
		for (Header h : headers) {
			System.out.println(h.getName() + "\t" + h.getValue());
		}
	}
}
